package com.winchain.chainbill.bill.biz.repurchase.service.impl;

import com.shell.bee.acct.transaction.po.AcctTxRec;
import com.shell.framework.command.CallbackContext;
import com.shell.framework.command.ChainCallback;
import com.winchain.chainbill.bill.base.po.CbOpt;
import com.winchain.chainbill.bill.base.po.CbRec;
import com.winchain.chainbill.bill.base.po.CbRpur;
import com.winchain.chainbill.bill.base.service.CbRpurService;
import com.winchain.chainbill.bill.biz.Bill;
import com.winchain.chainbill.bill.biz.receive.cmd.BillHldrCmd;
import com.winchain.chainbill.bill.biz.repurchase.cmd.BillDctMoveCmd;
import com.winchain.chainbill.bill.biz.repurchase.cmd.BillDctRpurCmd;
import com.winchain.chainbill.bill.biz.repurchase.cmd.BillRcv4RpurCmd;
import com.winchain.chainbill.bill.biz.repurchase.cmd.BillRpurCertCmd;
import com.winchain.chainbill.bill.biz.repurchase.cmd.BillRpurCmd;
import com.winchain.chainbill.bill.biz.repurchase.vo.BillRpur;
import com.winchain.chainbill.bill.biz.send.cmd.BillMoveCmd;
import com.winchain.chainbill.bill.biz.send.cmd.BillOptCmd;
import com.winchain.chainbill.chain.callback.CallbackService;
import com.winchain.chainbill.trade.cmd.TranCallbackCmd;
import com.winchain.chainbill.trade.cmd.TranRollbackCmd;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("billRpurPmtCallbackService")
public class BillRpurPmtCallbackServiceImpl implements CallbackService {
	@Autowired
	private CbRpurService cbRpurService;
	@Autowired
	private BillOptCmd<CbRpur> billOptCmd;
	@Autowired
	private BillMoveCmd<CbRpur> billMoveCmd;
	@Autowired
	private BillRpurCmd billRpurCmd;
	@Autowired
	private BillRpurCertCmd billRpurCertCmd;
	@Autowired
	private BillDctMoveCmd billSellMoveCmd;
	@Autowired
	private BillHldrCmd<CbRpur> billHldrCmd;
	@Autowired
	private BillRcv4RpurCmd billRcv4RpurCmd;
	@Autowired
	private TranCallbackCmd<Bill<CbRpur>> tranCallbackCmd;
	@Autowired
	private TranRollbackCmd<Bill<CbRpur>> tranRollbackCmd;
	@Autowired
	private BillDctRpurCmd billDctRpurCmd;

	public String getType() {
		return "cb_hld_pmt";
	}

	public void confirm(AcctTxRec tran) {
		String billId = tran.getBizId();

		BillRpur billRpur = this.cbRpurService.selectBillRpurByBillIdAndState(billId, "1");

		billRpur.setRpurState("3");

		Bill<CbRpur> bill = new Bill();
		bill.setRec(billRpur.getRec());
		bill.setSource(billRpur.getRpur());

		CbOpt opt = new CbOpt();
		opt.setCurState("S031");
		opt.setOptCode("EX11");
		opt.setRemark("������������������������������");

		CallbackContext<Bill<CbRpur>, AcctTxRec> context = new CallbackContext();
		context.setSource(bill);
		context.setTarget(tran);
		context.setValue("Operation", opt);

		context.setValue("OwnerId", billRpur.getBuyerId());
		context.setValue("OwnerName", billRpur.getBuyerNm());

		ChainCallback<Bill<CbRpur>, AcctTxRec> ccb = new ChainCallback();
		ccb.add(this.billOptCmd);
		ccb.add(this.billRpurCmd);
		ccb.add(this.billSellMoveCmd);
		ccb.add(this.billHldrCmd);
		ccb.add(this.billMoveCmd);
		if (!bill.getRec().getBillId().equals(bill.getRec().getPrntId())) {
			ccb.add(this.billRcv4RpurCmd);
		}
		ccb.add(this.billRpurCertCmd);
		ccb.add(this.tranCallbackCmd);

		ccb.add(this.billDctRpurCmd);

		ccb.callback(context);
	}

	public void cancel(AcctTxRec tran) {
		String billId = tran.getBizId();

		BillRpur billRpur = this.cbRpurService.selectBillRpurByBillIdAndState(billId, "1");

		Bill<CbRpur> bill = new Bill();
		bill.setRec(billRpur.getRec());
		bill.setSource(billRpur.getRpur());

		billRpur.setRpurState("2");

		CbOpt opt = new CbOpt();
		opt.setCurState("S031");
		opt.setOptCode("EX12");
		opt.setRemark("������������������������������");

		CallbackContext<Bill<CbRpur>, AcctTxRec> context = new CallbackContext();
		context.setSource(bill);
		context.setTarget(tran);
		context.setValue("Operation", opt);

		ChainCallback<Bill<CbRpur>, AcctTxRec> ccb = new ChainCallback();
		ccb.add(this.billOptCmd);
		ccb.add(this.billRpurCmd);
		ccb.add(this.billMoveCmd);

		ccb.add(this.tranRollbackCmd);
		ccb.callback(context);
	}
}
